﻿using EasyNet.Componets.Core.Http;
using EasyNet.Componets.Core.Utils;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EasyNet.Componets.Core.Locations.Baidu
{
    public class BaiduLocation : BaseLocation, ILocation
    {
       
        /// <summary>
        /// 根据城市地址获取位置
        /// </summary>
        /// <param name="address"></param>
        /// <param name="city"></param>
        /// <returns></returns>
        public LocationPoint GetLocationByAddress(string address, string city)
        {
            if (string.IsNullOrEmpty(address)) return null;

            var queryString = GetQueryString();
            queryString
                .Add("output", "json")
                .Add("address", address.Replace(" ", ""));

            if (!string.IsNullOrEmpty(city)) queryString.Add("city", city);

            try
            {
                //{"status":0,"result":{"location":{"lng":114.02597365732,"lat":22.546053546205},"precise":0,"confidence":14,"level":"\u57ce\u5e02"}}
                var json = HttpUtil.Get("http://api.map.baidu.com/geocoder/v2/?" + queryString, null);
                var msg = json.DeserializeFromJson<BaiduLocationMessage>();
                if (msg != null && msg.status == 0 && msg.result != null)
                {
                    return msg.result.location;
                }
            }
            catch
            {
            }

            return null;
        }

        /// <summary>
        /// 根据IP获取位置
        /// </summary>
        /// <param name="ip"></param>
        /// <returns></returns>
        public LocationPoint GetLocationByIp(string ip)
        {
            if (string.IsNullOrEmpty(ip)) return null;

            var queryString = GetQueryString();
            queryString.Add("coor", "bd09ll")
                .Add("ip", ip);

            try
            {
                //{"address":"CN|\u5409\u6797|\u957f\u6625|None|CERNET|0|0","content":{"address":"\u5409\u6797\u7701\u957f\u6625\u5e02","address_detail":{"city":"\u957f\u6625\u5e02","city_code":53,"district":"","province":"\u5409\u6797\u7701","street":"","street_number":""},"point":{"x":"125.31364243","y":"43.89833761"}},"status":0}
                var json = HttpUtil.Get("http://api.map.baidu.com/location/ip?" + queryString, null);

                var msg = json.DeserializeFromJson<BaiduLocationMessage>();
                if (msg != null && msg.status == 0 && msg.point != null)
                {
                    return msg.point.ConvertToLocation();
                }
            }
            catch
            {
            }

            return null;
        }
        /// <summary>
        /// 拼接查询字符串
        /// </summary>
        /// <returns></returns>
        private HttpQueryStringBuilder GetQueryString()
        {
            return new HttpQueryStringBuilder()
                .Add("ak", ConfigurationManager.AppSettings["Location_BaiduAk"] ?? "76q7hrjkjdxHMXdi68Fwcf4B");
        }
    }
}
